const box = document.getElementById("plug_box")
cssLink = document.createElement("link");
cssLink.href = "./plug/date.plug.css";
cssLink.rel = "stylesheet";
cssLink.type = "text/css";
async function main(){
    dsr_data = await window.plugAPI.readData("dsr")
    dsr_data = await deal_cf(dsr_data)
    for(d of dsr_data.day_list){
        date_split = d["date"].split("-")
        day_count = Math.ceil((new Date(d["date"]+" 0:00") - Date.now())/86400000)
        if(day_count<0){
            note = d["name"] + " 已经"
            color = "#ff983f"
        }else if(day_count > 0){
            note = d["name"] + " 还有"
            color = "#2196F3"
        }else{
            note = d["name"]
            color = "#55ccc9"
        }
        new_date = document.createElement("div")
        new_date.className = "dsr_rl"
        new_date.innerHTML=`
        <div class="rl_type" style="background-color: ${color};display: flex;">
            <div style="margin: auto;color: rgb(255,255,255)">${note}</div>
        </div>
        <div class="rl_date" style="height: 100px;display: flex;">
            <div style="margin: auto;"><h3 class="m_f">${Math.abs(day_count)}</h3></div>
        </div>
        `
        box.appendChild(new_date)
    }
    var currentDate = new Date();
    load_date = currentDate.toLocaleDateString();
}
async function update_show(){
    dsr_data = await window.plugAPI.readData("dsr")
    dsr_data = await deal_cf(dsr_data)
    for(let i=0;i<dsr_data.day_list.length;i++){
        d = dsr_data.day_list[i]
        date_split = d["date"].split("-")
        day_count = Math.ceil((new Date(d["date"]+" 0:00") - Date.now())/86400000)
        if(day_count<0){
            note = d["name"] + " 已经"
            color = "#ff983f"
        }else if(day_count > 0){
            note = d["name"] + " 还有"
            color = "#2196F3"
        }else{
            note = d["name"]
            color = "#55ccc9"
        }
        box.children[i].children[0].style.backgroundColor = color
        box.children[i].children[0].children[0].innerText = note
        box.children[i].children[1].children[0].innerText = Math.abs(day_count)
    }
    // var currentDate = new Date();
    // load_date = currentDate.toLocaleDateString();
    // for(dsr of document.querySelectorAll(".dsr_rl")){
    //     dsr.remove()
    // }
    // await main()
}
async function check(){
    await update_show()
    // var currentDate = new Date().toLocaleDateString();;
    // if(currentDate != load_date){
    //     await update_show()
    // }
}
async function deal_cf(dsr_data){
    var range_i=0
    for(d of dsr_data.day_list){
        day_count = Math.ceil((new Date(d["date"]+" 0:00") - Date.now())/86400000)
        console.log(day_count)
        console.log(d["name"])
        if(day_count<0){
            this_time=Date.parse(d["date"])
            console.log(this_time)
            if(d["repeat_mode"]=="不重复"){
                range_i+=1
                continue
            }
            if(d["repeat_mode"]=="每周重复"){
                this_time = new Date(d["date"]);
                this_time.setDate(this_time.getDate() + 7);
            }else if(d["repeat_mode"]=="每月重复"){
                this_time = new Date(d["date"]);
                this_time.setMonth(this_time.getMonth() + 1);
            }else if(d["repeat_mode"]=="每年重复"){
                this_time = new Date(d["date"]);
                this_time.setFullYear(this_time.getFullYear() + 1);
            }
            console.log(this_time)
            this_time=this_time.toLocaleDateString()
            this_time_list = this_time.split("/")
            for(i=1;i<3;i++){
                if(this_time_list[i].length==1){
                    this_time_list[i]="0"+this_time_list[i]
                }
            }
            d["date"]=this_time_list[0]+"-"+this_time_list[1]+"-"+this_time_list[2]
            dsr_data.day_list[range_i]=d
        }else{
            console.log("无需调整")
        }
        range_i+=1
    }
    await window.plugAPI.dumpData("dsr",dsr_data)
    return dsr_data
}
load_date = ""
async function run(){
    document.body.appendChild(cssLink);
    await main()
    setInterval(check,1000)
}
run()